import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/device/measure/measure_logic.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';

class MeasurePage extends StatelessWidget {

  final logic = Get.put(MeasureLogic());
  final state = Get.find<MeasureLogic>().state;

  @override
  Widget build(BuildContext context) {
    bool value = logic.read("measure_switch", defaultValue: false);
    if(value) {
      logic.switchAll(value);
      logic.switchMeasure(value);
    } else {
      logic.switchHeart(logic.read("measure_heart_value", defaultValue: false));
      logic.switchOxygen(logic.read("measure_oxygen_value", defaultValue: false));
      logic.switchBloodPressure(logic.read("measure_blood_pressure_value", defaultValue: false));
      logic.switchSugar(logic.read("measure_suger_value", defaultValue: false));
      logic.switchPressure(logic.read("measure_pressure_value", defaultValue: false));
      logic.switchTemp(logic.read("measure_temp_value", defaultValue: false));
      logic.switchMet(logic.read("measure_met_value", defaultValue: false, ));
      logic.switchFatigue(logic.read("measure_fatigue_value", defaultValue: false));
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(LocaleKeys.measure.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
        // leading: IconButton(
        //   padding: EdgeInsets.only(left: 15,),
        //   icon: Image.asset("assets/images/space/icon_appbar_arrow_back.png"),
        //   onPressed: () { Get.back(); },
        //   // child: Image.asset("images/icon_appbar_arrow_back.png"),
        // ),
        leadingWidth: 25,
      ),
      body: ListView(
        children: [
          Container(
            height: 58,
            padding: EdgeInsets.only(left: 59.w, right: 59.w),
            margin: EdgeInsets.fromLTRB(43.w, 100.h, 43.w, 39.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(LocaleKeys.measure_switch.ts, style: TextStyle(fontSize: 42.sp),),
                GetBuilder<MeasureLogic>(
                    id: "measure_switch",
                    builder: (_){
                      return Transform.scale(
                        scale: 0.7,
                        child: CupertinoSwitch(value: state.switchValue,
                            activeColor: ColorConst.activeColor,
                            onChanged: (value){
                              logic.save("measure_switch", value);
                              logic.save("measure_heart_value", value);
                              logic.switchAll(value);
                              logic.switchMeasure(value);
                            }),
                      );
                    })
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.fromLTRB(43.w, 39.h, 43.w, 39.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white
            ),
            child:Column(
              children: [
                _buildMeasureItem(LocaleKeys.measure_heart.ts, "heart_switch", onTap: (){
                  logic.save("measure_heart_value", !state.heartValue);
                  logic.switchHeart(!state.heartValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_oxygen.ts, "oxygen_switch", onTap: (){
                  logic.save("measure_oxygen_value", !state.oxygenValue);
                  logic.switchOxygen(!state.oxygenValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_blood_pressure.ts, "blood_pressure_switch", onTap: (){
                  logic.save("measure_blood_pressure_value", !state.bloodPressureValue);
                  logic.switchBloodPressure(!state.bloodPressureValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_sugar.ts, "suger_switch", onTap: (){
                  logic.save("measure_suger_value", !state.sugarValue);
                  logic.switchSugar(!state.sugarValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_pressure.ts, "pressure_switch", onTap: (){
                  logic.save("measure_pressure_value", !state.pressureValue);
                  logic.switchPressure(!state.pressureValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_temp.ts, "temp_switch", onTap: (){
                  logic.save("measure_temp_value", !state.tempValue);
                  logic.switchTemp(!state.tempValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_met.ts, "met_switch", onTap: (){
                  logic.save("measure_met_value", !state.metValue);
                  logic.switchMet(!state.metValue);
                }),
                const Divider(height: 1,),
                _buildMeasureItem(LocaleKeys.measure_fatigue.ts, "fatigue_switch", onTap: (){
                  logic.save("measure_fatigue_value", !state.fatigueValue);
                  logic.switchFatigue(!state.fatigueValue);
                }),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildMeasureItem(String title, String id, {onTap}) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child:Container(
          padding: EdgeInsets.fromLTRB(59.w, 30.h, 59.w, 30.h),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(title, style: TextStyle(fontSize: 42.sp),),
              GetBuilder<MeasureLogic>(
                  id: id,
                  builder: (_){
                    return Container(
                      decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.transparent),
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: logic.switchLogic(id)
                            ? Image(
                          image:const AssetImage('assets/images/device/icon_measure_checked.png'),
                          width: 60.w,
                          height: 60.w,
                        )
                            : Image(
                          image:const AssetImage('assets/images/device/icon_measure_unchecked.png'),
                          width: 60.w,
                          height: 60.w,
                        ),
                      ),
                    );
                  }),
            ],
          ),
        )
    );
  }
}
